
**************************************************************************************************;
**************************************************************************************************;
*This do file generates the following set of tables:
*Table 2: Authoritarian Parenting and Academic Achievement of Friends (Child Fixed Effects) 
*Table 3: Authoritarian Parenting and Dynamics of the Academic Achievement of Peers
*Appendix Table A-6: Authoritarian Parenting and Dynamics of Peer Achievement(Index Measure)
**************************************************************************************************;
**************************************************************************************************;

global controls "i.gender i.race age educ_mother_miss d_educ_mother_miss family_income_miss d_family_income_miss "

*Open data;
use $scratch/final_skills_peers.dta, clear

egen idgrade = group(idschool class)

egen school_size = count(idschool) , by(idschool)
egen grade_size = count(idgrade), by(idgrade)

recode H1WP2 (0=1) (1=0) , gen(authoritarian_parents)
recode H2WP2 (0=1) (1=0) , gen(authoritarian_parents_t2)


egen gpa = rowmean(cogn2 cogn3 cogn4 cogn5) 
egen gpa_t2 = rowmean(cogn2_t2 cogn3_t2 cogn4_t2 cogn5_t2) 

 
egen gpa_peers = rowmean(mean_peer_cogn2   mean_peer_cogn3    mean_peer_cogn4   mean_peer_cogn5)
egen gpa_peers_t2 = rowmean( mean_peer_cogn2_t2   mean_peer_cogn3_t2    mean_peer_cogn4_t2   mean_peer_cogn5_t2) 
 
 
replace race = 4 if race==.
replace gender = 3 if gender==.


gen family_income_miss = family_income 
qui sum family_income
replace family_income_miss = r(mean) if family_income==.

gen d_family_income_miss = 0
replace d_family_income_miss = 1 if family_income==.

gen educ_mother_miss = .
replace  educ_mother_miss = educ_mother if educ_mother!=.
replace educ_mother_miss = 0 if educ_mother==.

gen d_educ_mother_miss = 0
replace d_educ_mother_miss =1 if educ_mother==.

factor    inv4 inv6   inv9  , factors(1)
predict inv if e(sample) , bartlett
factor    inv4_t2 inv6_t2   inv9_t2  , factors(1)
predict inv_t2 if e(sample) , bartlett
gen delta_inv = inv_t2 - inv

*Saturated sample that include information about the dynamics (Wave I and Wave II) of peers;
*Sample described in Appendix Table A-1;
keep if class >=9 & class<=12
*Keep only saturated High-Schools in the sample;
keep if sat==1







************************************************************************************;
************************************************************************************;
*Table 2: Authoritarian Parenting and Academic Achievement of Friends (Child Fixed Effects) 
*Appendix Table A-6: uthoritarian Parenting and Dynamics of Peer Achievement(Index Measure)
************************************************************************************;
************************************************************************************;


preserve 

gen delta_authoritarian_parents = authoritarian_parents_t2 - authoritarian_parents
gen delta_gpa = gpa_t2 - gpa
gen delta_gpa_peers = gpa_peers_t2 - gpa_peers

gen gpa_new = gpa

gen gpa_interact_delta_gpa = gpa*delta_gpa

global regressors1 " delta_gpa_peers    $controls   "
global regressors2 " delta_gpa delta_gpa_peers $controls  "
global regressors3 " gpa_new gpa_interact_delta_gpa delta_gpa delta_gpa_peers $controls  "
global regressors4 "  delta_gpa_peers $controls  "
global regressors5 " delta_gpa delta_gpa_peers $controls  "
global regressors6 " gpa_new gpa_interact_delta_gpa delta_gpa delta_gpa_peers $controls  "

label variable delta_gpa "Change in Child's GPA"
label variable delta_gpa_peers "Change in Peer GPA"
label variable gpa_interact_delta_gpa "\multirow{2}{*}{Child's GPA (t-1) $\times$ Change in Child's GPA}"
xtset idgrade
qui xtreg delta_authoritarian_parents delta_gpa delta_gpa_peers $controls , fe 
gen est_sample = e(sample)



************************************************************************************;
*Table 2: Authoritarian Parenting and Academic Achievement of Friends (Child Fixed Effects) 
************************************************************************************;

forvalues j = 1(1)6{

if `j'<4{
reg delta_authoritarian_parents ${regressors`j'} if est_sample==1  ,  vce(cluster idschool)
est store est_`j'
qui sum delta_authoritarian_parents if e(sample)
qui estadd scalar mean = r(mean)

estadd local cont "Yes": est_`j'
estadd local c_fe "No": est_`j'

}

if `j'>=4{

xtreg delta_authoritarian_parents ${regressors`j'} if est_sample==1 , fe vce(cluster idschool)
est store est_`j'
qui sum delta_authoritarian_parents if e(sample)
qui estadd scalar mean = r(mean)
estadd local cont "Yes": est_`j'
estadd local c_fe "Yes": est_`j'
}

}



#delimit ;
esttab est_1 est_2 est_3 est_4 est_5 est_6   , 
nonotes nocons  replace label  
starlevels(* 0.10 ** 0.05 *** 0.01) b(%5.3f) se not keep(delta_gpa delta_gpa_peers gpa_interact_delta_gpa  ) order( delta_gpa_peers delta_gpa gpa_interact_delta_gpa )
stats(mean N N_clust cont c_fe , fmt(%9.3f %9.0g %9.0g %s %s) 
 labels("Mean Dependent Var." "Observations" "Clusters" "Controls" "Classroom F.E."))  
 prehead( " \begin{tabular}{@{\extracolsep{10pt}}lcccccc@{}}\hline \hline \\  ")  posthead(" &    \multicolumn{6}{c}{ Change in Authoritarian Style}  \\ \cline{2-7}\\ ")
 postfoot("\hline \hline \end{tabular}")  
 nomtitles
 ;
#delimit cr;
estimates clear
drop est_sample



local ind = 1
foreach x in H1WP1 H1WP2 H1WP3  H1WP6  {
recode `x' (0=1) (1=0), gen(author_proxy`ind')
local ++ind
}

local ind = 1
foreach x in  H2WP1 H2WP2 H2WP3  H2WP6  {
recode `x' (0=1) (1=0), gen(author_proxy`ind'_t2)
local ++ind
}

factor author_proxy1 author_proxy2 author_proxy3 author_proxy4  , factors(1)
predict author_factor if e(sample), bartlett
qui sum author_factor, de
replace author_factor = (author_factor-r(mean))/r(sd)

factor author_proxy1_t2 author_proxy2_t2 author_proxy3_t2 author_proxy4_t2  , factors(1)
predict author_factor_t2 if e(sample), bartlett
qui sum author_factor_t2, de
replace author_factor_t2 = (author_factor_t2-r(mean))/r(sd)

gen delta_author_factor = author_factor_t2 - author_factor

xtset idgrade
qui xtreg delta_author_factor delta_gpa delta_gpa_peers $controls , fe 
gen est_sample = e(sample)


************************************************************************************;
*Appendix Table A-6: uthoritarian Parenting and Dynamics of Peer Achievement(Index Measure)
************************************************************************************;


forvalues j = 1(1)6{

if `j'<4{
reg delta_author_factor ${regressors`j'} if est_sample==1  ,  vce(cluster idschool)
est store est_`j'
qui sum delta_author_factor if e(sample)
qui estadd scalar mean = r(mean)

estadd local cont "Yes": est_`j'
estadd local c_fe "No": est_`j'

}

if `j'>=4{

xtreg delta_author_factor ${regressors`j'} if est_sample==1 , fe vce(cluster idschool)
est store est_`j'
qui sum delta_author_factor if e(sample)
qui estadd scalar mean = r(mean)
estadd local cont "Yes": est_`j'
estadd local c_fe "Yes": est_`j'
}
}



#delimit ;
esttab est_1 est_2 est_3 est_4 est_5 est_6 , 
nonotes nocons  replace label  
starlevels(* 0.10 ** 0.05 *** 0.01) b(%5.3f) se not keep(delta_gpa delta_gpa_peers gpa_interact_delta_gpa  ) order( delta_gpa_peers delta_gpa gpa_interact_delta_gpa )
stats(mean N N_clust cont c_fe , fmt(%9.3f %9.0g %9.0g %s %s) 
 labels("Mean Dependent Var." "Observations" "Clusters" "Controls" "Classroom F.E."))  
prehead( " \begin{tabular}{@{\extracolsep{10pt}}lcccccccc@{}} \hline \hline \\  ")  posthead(" &    \multicolumn{6}{c}{ Change in Authoritarian Style}  &    \multicolumn{2}{c}{ Change in Investments} \\  &    \multicolumn{6}{c}{ }  &    \multicolumn{2}{c}{(for Authoritarian Parents at t-1)} \\ \cline{2-7} \cline{8-9} \\ ")
 postfoot("\hline \hline \end{tabular}")    
 nomtitles
 ;
#delimit cr;

restore





************************************************************************************;
************************************************************************************;
*Table 3: Authoritarian Parenting and Dynamics of the Academic Achievement of Peers
************************************************************************************;
************************************************************************************;
preserve

local ind = 1
foreach x in H1WP1 H1WP2 H1WP3  H1WP6  {
recode `x' (0=1) (1=0), gen(author_proxy`ind')
local ++ind
}

factor author_proxy1 author_proxy2 author_proxy3 author_proxy4  , factors(1)
predict author_factor if e(sample), bartlett
qui sum author_factor, de
replace author_factor = (author_factor-r(mean))/r(sd)

global regressors1 "authoritarian_parents      "
global regressors2 " authoritarian_parents gpa_peers "
global regressors3 " gpa gpa_peers authoritarian_parents   "
global regressors4 " authoritarian_parents    $controls"
global regressors5 " authoritarian_parents gpa_peers $controls"
global regressors6 " gpa gpa_peers authoritarian_parents   $controls"


xtset idgrade
qui xtreg authoritarian_parents gpa gpa_peers authoritarian_parents   $controls, fe 
gen est_sample = e(sample)

label variable authoritarian_parents "Authoritarian"
label variable gpa "Child's GPA"
label variable gpa_peers "Peer GPA"

forvalues j = 1(1)6{

if `j'<4{
xtreg gpa_peers_t2 ${regressors`j'}  if est_sample==1 , fe vce(cluster idschool)
est store est_`j'
qui sum gpa_peers_t2 if e(sample)
qui estadd scalar mean = r(mean)
estadd local cont "No": est_`j'
estadd local c_fe "Yes": est_`j'
}

if `j'>=4{

xtreg gpa_peers_t2 ${regressors`j'} if est_sample==1 , fe vce(cluster idschool)
est store est_`j'
qui sum gpa_peers_t2 if e(sample)
qui estadd scalar mean = r(mean)
estadd local cont "Yes": est_`j'
estadd local c_fe "Yes": est_`j'
}

}

#delimit ;
esttab est_1 est_2 est_3 est_4 est_5 est_6, 
nonotes nocons  replace label  
starlevels(* 0.10 ** 0.05 *** 0.01) b(%5.3f) se not keep(gpa gpa_peers authoritarian_parents  ) order(authoritarian_parents gpa_peers  gpa   )

stats(mean N N_clust cont c_fe , fmt(%9.3f %9.0g %9.0g %s %s) 
 labels("Mean Dependent Var." "Observations" "Clusters" "Controls" "Classroom F.E."))  
prehead( " \begin{tabular}{lcccccc} \hline \hline \\  ")  posthead(" &    \multicolumn{6}{c}{ Next Period Peer GPA}    \\ \hline \\ ")
 postfoot("\hline \hline \end{tabular}")  
 nomtitles
 ;
#delimit cr;

estimates clear
drop est_sample

restore



